﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace FruitVentDesignDemo.Extensions
{
    public static class EnumExtensions
    {
        public static string GetDescription(this Enum value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null) return value.ToString();

            // 获取字段信息
            FieldInfo field = type.GetField(name);
            if (field == null) return value.ToString();

            // 获取描述特性
            DescriptionAttribute attribute =
                Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;

            // 返回描述文本或默认名称
            return attribute?.Description ?? value.ToString();
        }
    }
}
